<?
/*
* USAGE:
*
* $shoppingCart = new ShoppingCartItem();
* $shoppingCart->add($i);                 // Adds $i items
* $shoppingCart->sub($i);                 // Removes $i items until 0
* $shoppingCart->setQuantity($i);         // Updates items quantity.
* $shoppingCart->setProperty($key, $val)  // Sets a particular property.
* $shoppingCart->getProperty($key)        // Gets a particular property.
*/

/*
 * @category   e-commerce
 * @package    ShoppingCart
 * @author     Alberto Pardo Saleme <apardo@adverit.com>
 * @copyright  2005 - AdVerit S.A. <www.adverit.com>
 * @version    Release: 1.0
 * @update     03/01/2006 04:04p.m.
 */



class ShoppingCartItem
{
    // {{{ properties

    /**
     * item's ID
     * @var mixed
     */
	public $id;

    /**
     * item's name
     * @var string
     */
	public $name = "";

    /**
     * item's quantity
     * @var int
     */
    public $quantity;

    /**
     * item's quantity
     * @var float
     */
	public $price=0;

    /**
     * item's surcharge. Used when dealing with
     * @var float
     */
	public $surcharge=0;

    /**
     * item's discount.
     * @var float
     */
	public $discount=0;

    /**
     * The list with item properties
     * @var array
     */
	public $properties = array();

	// }}}
    // {{{ constructor

    /**
     * This constructor sets up the ShoppingCart Item.
     * List of available options:
     *  - name:       The product Name.
     *  - price:      The product Price. If getTotalPrice is used, the price must me setted.
     *  - surcharge:  The product surchare. If getTotalPrice is used, the surcharge must me setted.
     *  - discount:   The product surchare. If getTotalPrice is used, the discount must me setted.
     *  - properties: An array of secondary properties
     *
     * @param id|string $id       The item's ID
     * @param int       $quantity The number of items contained
     * @param options   $opt      An array of options.
     * @return void
     */
	function __construct($id=null, $quantity=0, $opt=array())
	{
		$this->id         = $id;
		$this->quantity   = $quantity;
		$this->name       = $opt['name']? $opt['name'] : '';
		$this->price      = isset($opt['price'])? $opt['price'] : $this->price;
		$this->surcharge  = isset($opt['surcharge'])? $opt['surcharge'] : $this->surcharge;
		$this->discount   = isset($opt['discount'])? $opt['discount'] : $this->discount;
		$this->properties = is_array($opt['properties'])? $opt['properties'] : array();
	}

	// }}}
    // {{{ addItem()

    /**
     * Adds $i to quantity
     *
     * @param int  $num  integer to be added
     * @return void
     */
	function add($num=1)
	{
		$this->quantity += $num;
	}

	// }}}
    // {{{ sub()

    /**
     * Removes $i items until 0
     *
     * @param int  $num  integer to be substracted
     * @return void
     */
	function sub($num=1)
	{
		$this->quantity -= $num;
		$this->quantity = $this->quantity < 0? 0 : $this->quantity;
	}

	// }}}
    // {{{ setQuantity()

    /**
     * Updates items quantity.
     *
     * @param int  $num  integer that updates the item quantity
     * @return void
     */
	function setQuantity($num=1)
	{
		$this->quantity = $num;
	}

	// }}}
    // {{{ getProperty()

    /**
     * Gets a particular property.
     *
     * @param strign  $key  Property index
     * @return void
     */
	function getProperty($key)
	{
		if (array_key_exists($key, $this->properties))
			return $this->properties[$key];
		else
			return null;
	}

	// }}}
    // {{{ getPrice()

    /**
     * Gets a particular property.
     *
     * @param strign  $key  Property index
     * @return void
     */
	function getFinalPrice($round=2)
	{
	    $finalPrice = ($this->quantity * $this->price * ($this->discount==0? 1 : $this->discount)) + $this->surcharge;
		return round($finalPrice, $round);
	}

	// }}}
    // {{{ setProperty()

    /**
     * Sets a particular property.
     *
     * @param strign  $key  Property index
     * @param mixed   $val  Property value
     * @return void
     */
	function setProperty($key, $val)
	{
		$this->properties[$key] = $val;
	}

	// }}}

}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 */

?>
